{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "name": "GEN_8_DeOldify_Image.ipynb",
      "private_outputs": true,
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/cxbxmxcx/GenReality/blob/master/GEN_8_DeOldify_Image.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "S5Zx_zJ3Jruc"
      },
      "source": [
        "!git clone https://github.com/jantic/DeOldify.git DeOldify"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EZtSGovhJuPt"
      },
      "source": [
        "cd DeOldify"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YGNMmhSTJxWc"
      },
      "source": [
        "#NOTE:  This must be the first call in order to work properly!\r\n",
        "from deoldify import device\r\n",
        "from deoldify.device_id import DeviceId\r\n",
        "#choices:  CPU, GPU0...GPU7\r\n",
        "device.set(device=DeviceId.GPU0)\r\n",
        "\r\n",
        "import torch\r\n",
        "\r\n",
        "if not torch.cuda.is_available():\r\n",
        "    print('GPU not available.')"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-WN-O_sEJ0Ex"
      },
      "source": [
        "!pip install -r colab_requirements.txt"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "rVBTY_4wwfbq"
      },
      "source": [
        "#@title DEFINE DATASETS\n",
        "import os\n",
        "import time\n",
        "import datetime\n",
        "\n",
        "dataset_name = \"historic-bw\" #@param [\"historic-bw\"]\n",
        "\n",
        "datasets = { \"historic-bw\" : \n",
        "            { \"url\" : 'https://www.dropbox.com/s/kdvoiu4jfg899g4/historic-bw.zip?dl=1',\n",
        "                          }           \n",
        "            }\n",
        "\n",
        "img_root_folder = '/content/images'\n",
        "os.makedirs(img_root_folder, exist_ok=True)\n",
        "image_folder = img_root_folder + \"/%s\" % dataset_name \n",
        "color_image_folder = img_root_folder + \"/colorized-%s\" % dataset_name\n",
        "print(f\"Image data folders constructed {image_folder}\")\n",
        "os.makedirs(image_folder, exist_ok=True)\n",
        "os.makedirs(color_image_folder, exist_ok=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Jzze0ldCx1O_"
      },
      "source": [
        "#@title DOWNLOAD IMAGES\n",
        "if len(os.listdir(image_folder)) == 0:\n",
        "    from io import BytesIO\n",
        "    from urllib.request import urlopen\n",
        "    from zipfile import ZipFile\n",
        "    zipurl = datasets[dataset_name]['url']\n",
        "    with urlopen(zipurl) as zipresp:\n",
        "        with ZipFile(BytesIO(zipresp.read())) as zfile:        \n",
        "            zfile.extractall(img_root_folder)\n",
        "            print(f\"Downloaded & Extracted {zipurl}\")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6nQxdXR-KRTG"
      },
      "source": [
        "import fastai\r\n",
        "from deoldify.visualize import *\r\n",
        "import warnings\r\n",
        "warnings.filterwarnings(\"ignore\", category=UserWarning, message=\".*?Your .*? set is empty.*?\")"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AG6y78SzKYOp"
      },
      "source": [
        "!mkdir 'models'\r\n",
        "!wget https://data.deepai.org/deoldify/ColorizeArtistic_gen.pth -O ./models/ColorizeArtistic_gen.pth"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7cY6aODHKg51"
      },
      "source": [
        "!wget https://media.githubusercontent.com/media/jantic/DeOldify/master/resource_images/watermark.png -O ./resource_images/watermark.png"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WBRdIbZEKlpo"
      },
      "source": [
        "colorizer = get_image_colorizer(artistic=True)"
      ],
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yMd4wynFQIKr"
      },
      "source": [
        "#@title COLORIZE IMAGES  { run: \"auto\" }\r\n",
        "import glob\r\n",
        "from PIL import Image\r\n",
        "import ipywidgets as widgets\r\n",
        "from IPython.display import display\r\n",
        "from IPython.display import clear_output\r\n",
        "\r\n",
        "files = sorted(glob.glob(\"%s/*.jpg\" % image_folder))\r\n",
        "file_idx = 7 #@param {type:\"slider\", min:0, max:35, step:1}\r\n",
        "\r\n",
        "show_image_in_notebook(files[file_idx])\r\n",
        "image = colorizer.plot_transformed_image(files[file_idx])  \r\n",
        "show_image_in_notebook(image) "
      ],
      "execution_count": null,
      "outputs": []
    }
  ]
}